const db=wx.cloud.database({});
const course=db.collection('Course');

const NEW = "new";
const HOT = "hot";

Page({
  data: {
    courseData:[],
    sort:[
      {
        "icon":"/images/icons/bodybuilding.png",
        "name":'健身'
      },
      {
        "icon":"/images/icons/running.png",
        "name":'跑步'
      },
      {
        "icon":"/images/icons/ball.png",
        "name":'球类'
      },
      {
        "icon":"/images/icons/yoga.png",
        "name":'瑜伽'
      }
    ],
    titles:['新品','热门'],
    goods:[],
    // goods: {
    //   [NEW]: { list: [] },
    //   [HOT]: { list: [] },
    // },
    currentType: 'NEW',
    transgoods:[],
    sorts:0
  },
  
  onLoad: function (options) {
    var that=this
    //加载数据库数据(轮播图展示)
    db.collection('Course').where({
      type:'banner'
    }) .get({
      success:function(res){
        that.setData({
          courseData:res.data
        })
      }
    })

    db.collection('Course').where({
      hometype:'NEW'
    }).get({
      success:function(res){
        console.log(res.data)
        that.setData({
          goods:res.data
        })
      }
    })
  },

  handleiconClick(event){
    //确定是哪种分类
    console.log(event.currentTarget.dataset.index)
    
    this.setData({
      sorts:event.currentTarget.dataset.index
    })
    //跳到对应的分类页面
    wx.navigateTo({
      url: "/pages/sort/sort?sorts=" + this.data.sorts,
    })
  },

  handleItemClick(event){
    console.log(event)
    var that=this
    let currentType=''
    const index=event.detail.index;
    console.log(index)
    switch(index){
      case 0:{
        currentType=NEW
        db.collection('Course').where({
          hometype:'NEW'
        }).get({
          success:function(res){
            console.log(res.data)
    
            that.setData({
              goods:res.data
            })
          }
        })
        break
      }
      case 1:{
        currentType=HOT
        db.collection('Course').where({
          hometype:'HOT'
        }).get({
          success:function(res){
            console.log(res.data)
    
            that.setData({
              goods:res.data
            })
          }
        })
        break
      }
    }
    this.setData({
      currentType:currentType
    })
  },
  handleGoodClick(event){
    //事件参数的传递获取当前的商品（其实也可以获取index）
    const transgoods=event.currentTarget.dataset.item;
    
    this.setData({
      transgoods:transgoods
    })
  
    //把商品_id传过去（整个item好像传不了）
    wx.navigateTo({
      url: "/pages/detail/detail?itemId=" + this.data.transgoods._id,
    })
  }
})